/**
 * 集中器管理
 * @type 
 */
var Concentrator = {
	requestsURL		: {
		inputReq	: sys.basePath + 'concentratorAction!input.do',		//信息录入请求
		deleteReq	: sys.basePath + 'concentratorAction!delete.do'		//删除请求
	},
	inputForm		: null,		//录入表单
	inputWindow		: null,		//录入对话框
	fieldAnchor		: Common.config.fieldAnchor,
	dareaAnchor		: Common.config.dareaAnchor,
	windowWidth 	: document.body.clientWidth > 460 ? 460 : document.body.clientWidth - 10,
	windowHeight 	: document.body.clientHeight > 380 ? 380 : document.body.clientHeight - 10,
	optype			: null,
	getInputForm	: function () {
		if (this.inputForm != null) {
			return this.inputForm;
		}
		this.inputForm = new Ext.FormPanel({
			url: this.requestsURL.inputReq,
			region: 'center',
			bodyStyle: 'padding:5px; overflow-x:visible;overflow-y:scroll',
			autoScroll: true,
			border    : false,
	        frame: false,
	        labelAlign: 'right',
	        labelWidth: 86,	        
         	items: [{
        		xtype: 'panel',
        		//title: '基本信息',
        		autoHeight: true,
        		border: false,
            	items: [{
	            	layout: 'column',border: false, labelSeparator: ':',
	                defaults:{layout: 'form', border: false, columnWidth: 1.0},
	                items: [{
	                	items:[{
	                		xtype: 'hidden', id: 'concentratorid', name: 'concentrator.concentratorid'
	                	},{
	                		xtype: 'combo', id: 'type', hiddenName: 'concentrator.type', anchor: this.fieldAnchor,
	                		fieldLabel: Common.config.redStar + '类型', readOnly: false, allowBlank: false,
			                mode: 'local', triggerAction: 'all', valueField: 'value', displayField: 'text',
			                readOnly: false, resizable: true,
			                store: new Ext.data.SimpleStore({
								fields: ['value', 'text'], data: DataDict.typeArray
							})
	                	}]
	                },{
	                	items:[{
	                		xtype: 'textfield', id: 'model', name: 'concentrator.model', anchor: this.fieldAnchor,
	                		fieldLabel: Common.config.redStar + '规格', readOnly: false, allowBlank: false
	                	}]
	                },{
	                	items:[{
	                		xtype: 'combo', id: 'status', hiddenName: 'concentrator.status', anchor: this.fieldAnchor,
	                		fieldLabel: Common.config.redStar + '状态', readOnly: false, allowBlank: false,
			                mode: 'local', triggerAction: 'all', valueField: 'value', displayField: 'text',
			                readOnly: false, resizable: true,
			                store: new Ext.data.SimpleStore({
								fields: ['value', 'text'], data: DataDict.statusArray
							})
	                	}]
	                },{
	                	items:[{
	                		xtype: 'combo', id: 'isavailable', hiddenName: 'concentrator.isavailable', anchor: this.fieldAnchor,
	                		fieldLabel: Common.config.redStar + '是否启用', readOnly: false, allowBlank: false,
			                mode: 'local', triggerAction: 'all', valueField: 'value', displayField: 'text',
			                readOnly: false, resizable: true,
			                store: new Ext.data.SimpleStore({
								fields: ['value', 'text'], data: DataDict.isavailableArray
							})
	                	}]
	                },{
	                	items:[{
	                		xtype: 'textfield', id: 'tel', name: 'concentrator.tel', anchor: this.fieldAnchor,
	                		fieldLabel: Common.config.redStar + '电话号码', readOnly: false, allowBlank: false
	                	}]
	                },{
	                	items:[{
	                		xtype: 'textfield', id: 'pwd', name: 'concentrator.pwd', anchor: this.fieldAnchor,
	                		fieldLabel: Common.config.redStar + '密码', readOnly: false, allowBlank: false
	                	}]
	                },{
	                	items:[{
	                		xtype: 'numberfield', id: 'currentgrps', name: 'concentrator.currentgrps', anchor: this.fieldAnchor,
	                		fieldLabel: '剩余GPRS流量', readOnly: false, allowBlank: true, minValue: 0
	                	}]
	                },{
	                	items:[{
	                		xtype: 'numberfield', id: 'alarmgprs', name: 'concentrator.alarmgprs', anchor: this.fieldAnchor,
	                		fieldLabel: '预警GPRS流量', readOnly: false, allowBlank: true, minValue: 0
	                	}]
	                },{
						columnWidth:1.0,
	                	items:[{
	                		xtype: 'textarea', id: 'remark', name: 'concentrator.remark', anchor: this.fieldAnchor,
	                		fieldLabel: '备注', readOnly: false, allowBlank: true
	                	}]
	                }]
            	}]
         	}]
		});
		return this.inputForm;
	},
	showInputWindow : function (title) {
		if (this.inputWindow == null) {
			var inputForm = this.getInputForm();
			this.inputWindow = new Ext.Window({ //定义对话框
				title: '集中器信息录入',
				closeAction : 'hide',
				modal : true,
				shadow : true,
				closable : true,
				plain    : true,
				layout : 'fit',
				width : this.windowWidth,
				height : this.windowHeight,
				items:[inputForm],
				buttons : [{
					text :'保存',
					iconCls: 'icon_save',
					scope: this,
					handler: this.onSave
				},{
					text :'关闭',
					iconCls: 'icon_close',
					scope: this,
					handler: function(){ this.inputWindow.hide();}
				}]
			});
		}
		this.inputWindow.show();
		this.inputWindow.setTitle(title || "集中器信息录入");
	},
	onSave : function () {
		var thisForm = this.getInputForm().getForm();
		if (!thisForm.isValid()) {
			return;
		}
		Common.showProcessMsgBox("信息保存中，请稍后...");
		thisForm.submit({
            success: function(form, action){
            	Common.hideProcessMsgBox();
                Ext.topShow.msg('成功提示', action.result.msg);
                Concentrator.inputWindow.hide();
                dataGrid.getStore().reload();
            },
            failure : function (form, action) {
            	Common.hideProcessMsgBox();
            	var failMsg = Ext.isEmpty(action.result) ? "集中器信息保存失败." : action.result.msg;
        	 	Ext.Msg.show({
   	                title: '失败提示',
   	                msg: failMsg || '集中器信息保存失败.',
   	                width: 180,
   	                modal: false,
   	                buttons : Ext.Msg.OK,
   	                icon: Ext.MessageBox.ERROR
   	            });
            }
        });
	},
	onAdd : function () {		//新增
		Concentrator.optype	= Common.optype.add;
		Concentrator.showInputWindow("新增集中器");
		Concentrator.getInputForm().getForm().reset();
	},
	onUpdate : function () {	//修改
		if (dataGrid.getSelectionModel().getCount() != 1) {
            Ext.Msg.alert("提示信息", "请选择一条记录!");
            return;
        }
		Concentrator.optype	= Common.optype.update;
		var selectedRecord = dataGrid.getSelectionModel().getSelected();
		Concentrator.showInputWindow("修改集中器信息");
		var thisForm = Concentrator.getInputForm().getForm();
		thisForm.reset();
		thisForm.setValues(selectedRecord.data);
	},
	onDelete : function () {	//删除集中器
		var selectedRecordArray = dataGrid.getSelectionModel().getSelections();
		if(selectedRecordArray.length < 1){
     		Ext.Msg.alert("提示信息", "请选择要删除的记录!");
     		return;
      	}
		Concentrator.optype	= Common.optype.del;
        Ext.Msg.confirm("确认提示", "确定删除选定的记录?", function(btn){
         	if(btn == 'yes'){
              	var concentratorids = "";
              	for(var i = 0;i < selectedRecordArray.length;i++){
              		concentratorids += selectedRecordArray[i].get("concentratorid") + ",";
        	    }
        	    if(concentratorids.length > 0){
        	    	concentratorids = concentratorids.substring(0, concentratorids.length - 1);
        	    }
				Common.showProcessMsgBox('数据删除中，请稍后...');
				Ext.Ajax.request({
					method:'POST', 
					timeout:0,
					url : Concentrator.requestsURL.deleteReq,
					params : {
						concentratorids : concentratorids
					},
					success : function(response, options) {
						var result = Ext.util.JSON.decode(response.responseText);
						Ext.MessageBox.hide();
						if (result.success) {
					 		Ext.topShow.msg("成功提示", result.msg);
					 		dataGrid.getStore().reload();
		   			   	} else {
		   			   		Ext.Msg.show({
		       	                title: '失败提示',
		       	                msg: result.msg,
		       	                width: 180,
		       	                modal: false,
		       	                buttons : Ext.Msg.OK,
		       	                icon: Ext.MessageBox.ERROR
		       	            });
		       			}
					},
					failure : function() {
						Ext.MessageBox.hide();
		   			   	Ext.Msg.show({
		   	                title: '失败提示',
		   	                msg: '删除数据时发生异常.',
		   	                width: 180,
		   	                modal: false,
		   	                buttons : Ext.Msg.OK,
		   	                icon: Ext.MessageBox.ERROR
		   	            });
					}
				});
         	}
        });	
	}
};